<?php

class ProductWaitCheckController extends BaseController
{
	
	//待审核产品列表页
	public function actionAdmin()
	{
		$model=new Product('search');
		$model->unsetAttributes();  // clear any default values
		
		$model->zp_status = 1;//待审核的产品
		
		if(isset($_GET['Product'])){
			$model->attributes=$_GET['Product'];
			if($_GET['Product']['cateid3']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid3'];
			}elseif($_GET['Product']['cateid2']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid2'];
			}elseif($_GET['Product']['cateid1']>0){
				$model->catgory_id = (int)$_GET['Product']['cateid1'];
			}
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	
	//产品审试核详细页
	public function actionCheckStatus($id){
		$model = $this->loadModel($id);
		//不是待审核产品，直接跳出
		if($model->zp_status !=1){
			throw new CHttpException(404,'The requested page does not exist.');
		}
		
		if(isset($_POST['Product'])){
			$model->attributes = $_POST['Product'];
			
			if(isset($_POST['pass'])){
				$model->zp_status=2;//审核通过
				$message = "你发布的产品：".$model->zp_name."审核通过！";
			}elseif(isset($_POST['nopass'])){
				$model->zp_status=3;//审核不通过
				$model->zp_nostatus = trim($model->zp_nostatus);
				$message = "你发布的产品：".$model->zp_name."审核不通过！<Br>不通过的理由是：".$model->zp_nostatus;
			}
			//审核不通过，理由不能为空
			if(isset($_POST['nopass'])&&empty($model->zp_nostatus)){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，审核不通过，理由不能为空！'));
				exit;
			}
			if($model->save()){
				//判断系统设计，是否需要发送邮件
				if(F::setting("product_check_sent_mail")==1){
					//发送邮件
					EmailTemplates::model()->sentMail($model->zm_id,9,array('message'=>$message));
				}
				//发送消息
				MsgLog::sendMemberInfo($model->zm_id,$message);
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'恭喜，产品审核成功',
						'links'=>array(
								array('继续审核产品',$this->pre_module_url.'/productWaitCheck/admin'),
								array('前台浏览产品','/home/product/detail/id/'.$model->zp_id),
						),
						));
				exit;
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，产品审核操作失败，请稍后再试！'));
				exit;
			}
		}
		
	    $this->render('product_info',array(
				'model'=>$model,
		));
	}
	
	/**
	 *实例化模型
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Product::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

}